//
//  WJQBaseViewController.m
//  TripTogether
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "WJQBaseViewController.h"

@interface WJQBaseViewController ()

@property (nonatomic, strong) MBProgressHUD *hub;

@end

@implementation WJQBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 改navg左右字的颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    // 改navg标题字的颜色 和 大小
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor] ,NSFontAttributeName:[UIFont boldSystemFontOfSize:17.f]};
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.32 green:0.74 blue:0.8 alpha:1];
    

    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    //返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_nav_back_button"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

}
- (void)leftAction:(UIBarButtonItem *)sender{

    [self.navigationController popViewControllerAnimated:YES];
}
- (void)NetworkError:(Block)block
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"网络错误"message:@"请检查网络错误" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        block();
    }];
    
    [alertC addAction:action1];
    [self presentViewController:alertC animated:YES completion:nil];
}
- (void)MBShow
{
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"正在加载...";
}
- (void)MBHide
{
    [self.hub hide:YES];
}
// 登陆注册 模态
- (void)createBackButton
{
    UIButton *backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    backBtn.backgroundColor = [UIColor orangeColor];
    [backBtn setImage:[UIImage imageNamed:@"icon_nav_back_button@2x.png"] forState:(UIControlStateNormal)];
    [self.view addSubview:backBtn];
    [backBtn addTarget:self action:@selector(backBtn:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.offset(0);
        make.top.equalTo(@20);
        make.size.mas_equalTo(CGSizeMake(49, 80));
    }];

}
- (void)backBtn:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}



// 正则表达式, 判断手机号
- (BOOL)checkTel:(NSString *)str

{
    
    if ([str length] == 0) {

        
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示"message:@"手机号不能为空" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        }];
        
        [alertC addAction:action1];
        [self presentViewController:alertC animated:YES completion:nil];
        
        
        return NO;
        
    }

    //1[0-9]{10}
    
    //^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$
    
    //    NSString *regex = @"[0-9]{11}";
    
    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL isMatch = [pred evaluateWithObject:str];
    
    if (!isMatch) {

        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示"message:@"请输入正确的手机号码" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertC addAction:action1];
        [self presentViewController:alertC animated:YES completion:nil];

        
        
        return NO;
        
    }
    
    
    
    return YES;
    
}

// 密码
- (BOOL)checkPassword:(NSString *)passWord
{
    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:passWord];
}
// 用户名
- (BOOL)checkUserName:(NSString *)nickname
{
    NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,10}$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    return [passWordPredicate evaluateWithObject:nickname];
}
- (void)alertControllerWithTitle:(NSString *)title message:(NSString *)message
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertC addAction:action1];
    [self presentViewController:alertC animated:YES completion:nil];

}
- (void)alertControllerWithTitle:(NSString *)title message:(NSString *)message block:(Block)block
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        block();
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertC addAction:action2];
    [alertC addAction:action1];
    [self presentViewController:alertC animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
